<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI模型API密钥配置工具</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#4080FF',
                        success: '#00B42A',
                        warning: '#FF7D00',
                        danger: '#F53F3F',
                        info: '#86909C',
                        light: '#F2F3F5',
                        dark: '#1D2129',
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                },
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .card-shadow {
                box-shadow: 0 2px 14px 0 rgba(0, 0, 0, 0.08);
            }
            .input-focus {
                @apply focus:border-primary focus:ring-1 focus:ring-primary focus:outline-none;
            }
        }
    </style>
</head>
<body class="bg-gray-100 font-inter min-h-screen">
    <div class="max-w-4xl mx-auto px-4 py-8 md:py-12">
        <div class="text-center mb-8">
            <h1 class="text-[clamp(1.75rem,4vw,2.5rem)] font-bold text-dark mb-2">AI模型API密钥配置工具</h1>
            <p class="text-info text-lg">快速配置DeepSeek等AI模型的API密钥，启用智能生成测试用例功能</p>
        </div>

        <div class="bg-white rounded-xl card-shadow p-6 mb-8">
            <div class="mb-6">
                <h2 class="text-xl font-semibold text-dark mb-4 flex items-center">
                    <i class="fa fa-cog mr-2 text-primary"></i> 当前配置信息
                </h2>
                <div id="current-config" class="bg-light rounded-lg p-4 font-mono text-sm min-h-[150px] overflow-auto">
                    加载中...
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-8">
            <!-- DeepSeek-R1 配置 -->
            <div class="bg-white rounded-xl card-shadow p-6">
                <h2 class="text-xl font-semibold text-dark mb-4 flex items-center">
                    <i class="fa fa-robot mr-2 text-primary"></i> DeepSeek-R1
                </h2>
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-dark mb-1">API Key</label>
                        <input type="password" id="deepseek-api-key" class="w-full px-4 py-2 border border-gray-300 rounded-lg input-focus" placeholder="请输入DeepSeek API Key">
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-dark mb-1">API Base URL</label>
                        <input type="text" id="deepseek-api-base" value="https://api.deepseek.com" class="w-full px-4 py-2 border border-gray-300 rounded-lg input-focus" placeholder="API Base URL">
                    </div>
                    <div class="flex justify-between pt-2">
                        <button id="save-deepseek-btn" class="bg-primary text-white px-4 py-2 rounded-lg hover:bg-secondary transition-colors duration-200 flex items-center">
                            <i class="fa fa-save mr-1"></i> 保存配置
                        </button>
                        <button id="set-deepseek-default" class="bg-success text-white px-4 py-2 rounded-lg hover:bg-green-600 transition-colors duration-200 flex items-center">
                            <i class="fa fa-check mr-1"></i> 设为默认
                        </button>
                    </div>
                </div>
            </div>

            <!-- 模型选择器 -->
            <div class="bg-white rounded-xl card-shadow p-6">
                <h2 class="text-xl font-semibold text-dark mb-4 flex items-center">
                    <i class="fa fa-sliders mr-2 text-primary"></i> 模型设置
                </h2>
                <div class="space-y-6">
                    <div>
                        <label class="block text-sm font-medium text-dark mb-1">默认模型</label>
                        <select id="default-model" class="w-full px-4 py-2 border border-gray-300 rounded-lg input-focus">
                            <option value="deepseek-r1">DeepSeek-R1</option>
                            <option value="doubao">豆包</option>
                            <option value="tongyiqianwen">通义千问</option>
                            <option value="chatgpt">ChatGPT</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-dark mb-1">调用超时时间</label>
                        <div class="flex items-center">
                            <input type="number" id="timeout" min="10" max="300" step="5" value="60" class="w-full px-4 py-2 border border-gray-300 rounded-lg input-focus">
                            <span class="ml-2 text-info">秒</span>
                        </div>
                    </div>
                    <button id="save-settings-btn" class="w-full bg-primary text-white px-4 py-2 rounded-lg hover:bg-secondary transition-colors duration-200 flex items-center justify-center">
                        <i class="fa fa-cog mr-1"></i> 保存所有设置
                    </button>
                </div>
            </div>
        </div>

        <div class="bg-warning/10 border border-warning/30 rounded-lg p-4 mb-8">
            <h3 class="text-warning font-semibold mb-2 flex items-center">
                <i class="fa fa-exclamation-triangle mr-2"></i> 使用提示
            </h3>
            <ul class="list-disc list-inside text-dark space-y-1 text-sm">
                <li>API密钥将存储在浏览器的本地存储中，不会上传到服务器</li>
                <li>配置完成后，请刷新应用页面使配置生效</li>
                <li>如果没有DeepSeek API密钥，请前往DeepSeek官方网站申请</li>
                <li>密钥配置错误时，智能生成测试用例功能将无法正常使用</li>
            </ul>
        </div>

        <div class="text-center text-info text-sm">
            <p>配置工具版本 1.0 | <a href="/api-key-config-guide.html" class="underline hover:text-primary">查看详细配置指南</a></p>
        </div>
    </div>

    <script>
        // 存储键名
        const STORAGE_KEY = 'aiModelSettings';
        
        // 默认配置
        const DEFAULT_CONFIG = {
            defaultModel: 'deepseek-r1',
            models: {
                'deepseek-r1': {
                    apiKey: '',
                    apiBase: 'https://api.deepseek.com'
                },
                'doubao': {
                    apiKey: '',
                    apiBase: 'https://api.doubao.com'
                },
                'tongyiqianwen': {
                    apiKey: '',
                    apiBase: 'https://api.tongyi.aliyun.com'
                },
                'chatgpt': {
                    apiKey: '',
                    apiBase: 'https://api.openai.com'
                }
            },
            timeout: 60
        };

        // 加载配置
        function loadConfig() {
            try {
                const savedConfig = localStorage.getItem(STORAGE_KEY);
                if (savedConfig) {
                    return JSON.parse(savedConfig);
                }
            } catch (error) {
                console.error('加载配置失败:', error);
            }
            return DEFAULT_CONFIG;
        }

        // 保存配置
        function saveConfig(config) {
            try {
                localStorage.setItem(STORAGE_KEY, JSON.stringify(config));
                showMessage('配置保存成功！', 'success');
                return true;
            } catch (error) {
                console.error('保存配置失败:', error);
                showMessage('配置保存失败，请稍后重试', 'error');
                return false;
            }
        }

        // 显示配置信息
        function showCurrentConfig() {
            const config = loadConfig();
            const configElement = document.getElementById('current-config');
            
            // 格式化配置为可读的JSON
            const formattedConfig = JSON.stringify(config, null, 2);
            configElement.textContent = formattedConfig;
        }

        // 填充表单
        function populateForm() {
            const config = loadConfig();
            
            // 填充DeepSeek配置
            if (config.models && config.models['deepseek-r1']) {
                document.getElementById('deepseek-api-key').value = config.models['deepseek-r1'].apiKey || '';
                document.getElementById('deepseek-api-base').value = config.models['deepseek-r1'].apiBase || DEFAULT_CONFIG.models['deepseek-r1'].apiBase;
            }
            
            // 填充默认模型
            document.getElementById('default-model').value = config.defaultModel || DEFAULT_CONFIG.defaultModel;
            
            // 填充超时时间
            document.getElementById('timeout').value = config.timeout || DEFAULT_CONFIG.timeout;
        }

        // 显示消息提示
        function showMessage(message, type = 'info') {
            // 创建消息元素
            const messageElement = document.createElement('div');
            messageElement.className = `fixed top-4 right-4 px-4 py-3 rounded-lg shadow-lg transform transition-all duration-300 translate-x-full z-50 flex items-center`;
            
            // 设置不同类型的样式
            if (type === 'success') {
                messageElement.classList.add('bg-success/10', 'border', 'border-success/30', 'text-success');
                messageElement.innerHTML = `<i class="fa fa-check-circle mr-2"></i>${message}`;
            } else if (type === 'error') {
                messageElement.classList.add('bg-danger/10', 'border', 'border-danger/30', 'text-danger');
                messageElement.innerHTML = `<i class="fa fa-exclamation-circle mr-2"></i>${message}`;
            } else {
                messageElement.classList.add('bg-primary/10', 'border', 'border-primary/30', 'text-primary');
                messageElement.innerHTML = `<i class="fa fa-info-circle mr-2"></i>${message}`;
            }
            
            // 添加到文档
            document.body.appendChild(messageElement);
            
            // 显示消息
            setTimeout(() => {
                messageElement.classList.remove('translate-x-full');
                messageElement.classList.add('translate-x-0');
            }, 10);
            
            // 3秒后隐藏消息
            setTimeout(() => {
                messageElement.classList.remove('translate-x-0');
                messageElement.classList.add('translate-x-full');
                setTimeout(() => {
                    document.body.removeChild(messageElement);
                }, 300);
            }, 3000);
        }

        // 事件监听器
        document.addEventListener('DOMContentLoaded', () => {
            // 初始化
            showCurrentConfig();
            populateForm();
            
            // 保存DeepSeek配置
            document.getElementById('save-deepseek-btn').addEventListener('click', () => {
                const config = loadConfig();
                config.models = config.models || {};
                config.models['deepseek-r1'] = {
                    apiKey: document.getElementById('deepseek-api-key').value,
                    apiBase: document.getElementById('deepseek-api-base').value
                };
                saveConfig(config);
                showCurrentConfig();
            });
            
            // 设置DeepSeek为默认模型
            document.getElementById('set-deepseek-default').addEventListener('click', () => {
                const config = loadConfig();
                config.defaultModel = 'deepseek-r1';
                saveConfig(config);
                document.getElementById('default-model').value = 'deepseek-r1';
                showCurrentConfig();
            });
            
            // 保存所有设置
            document.getElementById('save-settings-btn').addEventListener('click', () => {
                const config = loadConfig();
                config.defaultModel = document.getElementById('default-model').value;
                config.timeout = parseInt(document.getElementById('timeout').value) || DEFAULT_CONFIG.timeout;
                saveConfig(config);
                showCurrentConfig();
            });
        });
    </script>
</body>
</html>